package com.dzm.content.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzm.constants.ClientConstant;
import com.dzm.content.entity.User;
import com.dzm.content.service.UserService;
import com.dzm.content.mapper.UserMapper;
import com.dzm.content.vo.LoginVo;
import com.dzm.except.Assert;
import com.dzm.to.LoginUser;
import com.dzm.to.UserTo;
import com.dzm.utils.JwtUtil;
import com.dzm.utils.ResponseEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author Dzm
* @description 针对表【user】的数据库操作Service实现
* @createDate 2024-09-13 19:26:48
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Override
    public LoginUser getLoginUser(LoginVo vo) {
        String username = vo.getUsername();
        String password = SecureUtil.md5(vo.getPassword());

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username)
                .eq(User::getPassword, password);

        User user = this.getOne(wrapper);
        Assert.notNull(user, ResponseEnum.LOGIN_EXCEPTION);
        //获取用户权限
        user.setPermission(baseMapper.getAllPermission(user.getUsername()));

        LoginUser loginUser = new LoginUser();
        UserTo userTo = new UserTo();
        BeanUtils.copyProperties(user, userTo);
        loginUser.setUser(userTo);

        return loginUser;
    }

    @Override
    public Map<String, Object> getUserInfo(String authorization) {
        String token = authorization.replaceFirst(ClientConstant.CLIENT_TOKEN_PRE, "");
        Map<String, Object> claims = JwtUtil.parseToken(token);
        String username = (String) claims.get("username");

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("name", username);
        resultMap.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        return resultMap;
    }
}




